package com.songlanyun.modules.withdraw.controller.admin;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.songlanyun.common.annotation.AuthIgnore;
import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.annotation.SysLog;
import com.songlanyun.common.enums.WithdrawConstant;
import com.songlanyun.common.utils.DateUtils;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.common.validator.ValidatorUtils;
import com.songlanyun.common.validator.group.UpdateGroup;
import com.songlanyun.modules.shop.model.vo.GetShopWithdrawVO;
import com.songlanyun.modules.shop.model.vo.ShopWithdrawVO;
import com.songlanyun.modules.user.controller.AbstractController;
import com.songlanyun.modules.withdraw.entity.Withdraw;
import com.songlanyun.modules.withdraw.model.dto.ShopWithdrawDTO;
import com.songlanyun.modules.withdraw.model.dto.WithdrawDTO;
import com.songlanyun.modules.withdraw.service.WithdrawService;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 提现列表
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2020-11-20 17:47:55
 */
@RestController
@RequestMapping("admin/withdraw")
public class WithdrawAdminController extends AbstractController {
    @Autowired
    private WithdrawService withdrawService;

    /**
     * 平台查看所有列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = withdrawService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 商家提现列表
     */
    @GetMapping("/shopList")
    public R shopList(@RequestParam Map<String, Object> params) {
        params.put("user_type", WithdrawConstant.UserType.SHOP);
        params.put("identity_id", getShopId(true));

        PageUtils page = withdrawService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        Withdraw withdraw = withdrawService.getById(id);

        return R.ok().put("data", withdraw);
    }

    /**
     * 商家所得提现
     *
     * @return
     */
    @SysLog("商家所得提现")
    @RepeatSubmit(value = "3")
    @PostMapping("/shopWithdraw")
    public R shopWithdraw(@RequestBody ShopWithdrawDTO dto) {
        ValidatorUtils.validateEntity(dto);
        Long shopId = getShopId(true);//商家id
        withdrawService.shopWithdraw(dto, shopId);

        return R.ok();
    }

    /**
     * 计算商家所得提现
     *
     * @return
     */
    @SysLog("计算商家所得提现")
    @RepeatSubmit(value = "3")
    @PostMapping("/getShopWithdraw")
    public R getShopWithdraw(@RequestBody ShopWithdrawDTO dto) {
        ValidatorUtils.validateEntity(dto);
        Long shopId = getShopId(true);//商家id
        GetShopWithdrawVO vo = withdrawService.getShopWithdraw(dto, shopId);

        return R.ok().put("data",vo);
    }


    /**
     * 平台提现审核
     *
     * @return
     */
    @SysLog("平台提现审核")
    @PostMapping("/withdrawCheck")
    public R withdrawCheck(@RequestBody WithdrawDTO dto) {
        verifyAdmin();
        ValidatorUtils.validateEntity(dto, UpdateGroup.class);

        dto.setVerifyMan(getUser().getNickName());
        withdrawService.withdrawCheck(dto);
        return R.ok();
    }

    /**
     * 平台确认打款  -- 生成平台账单 -- 绿马残联项目不需要确认打款
     *
     * @return
     */
    @SysLog("平台确认打款")
    @RepeatSubmit("3")
    @PostMapping("/remit")
    public R remit(@RequestBody WithdrawDTO dto) {
        verifyAdmin();

        if (StringUtils.isBlank(dto.getRemitMan())) {
            dto.setRemitMan(getUser().getNickName());
        }
        withdrawService.remit(dto);
        return R.ok();
    }


    /**
     * 统计商家所得
     *
     * @return
     */
    @RequestMapping("/statistics/bill")
    //@RequiresPermissions("gains:shopordergains:list")
    public R statistics() {
        ShopWithdrawVO withdrawAmount = withdrawService.statistics(getShopId(true));
        return R.ok().put("data", withdrawAmount);
    }

    /**
     * 平台提现列表导出
     *
     * @param params
     * @param response
     * @throws Exception
     */
    @GetMapping("/export")
    @AuthIgnore
    //@RequiresPermissions("usergains:distributionuserordergains:list")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        Long adminId = this.getAdminByToken(response);//校验token

        List<Withdraw> withdraws = withdrawService.exportList(params);
        String fileName = "申请提现记录" + DateUtils.format(new Date(), "yyyy-MM-dd-HHmmss") + "_" + adminId + "." + "xls";
        ExportParams exportParams = new ExportParams("申请提现记录导出(" + withdraws.size() + ")", "申请提现记录");

        Workbook sheets = ExcelExportUtil.exportExcel(exportParams, Withdraw.class, withdraws);
        response.setContentType(new MimetypesFileTypeMap().getContentType("application/octet-stream"));// 设置response内容的类型

        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息
        sheets.write(response.getOutputStream());
        sheets.close();
    }


}
